# fp = open("hh.txt")
#
# print(fp.read())
# import chardet
# fp = open("hh.txt","rb")
# print(chardet.detect(fp.read()))

# fp = open("hh.txt", mode="w")
# fp.write("aaaa")
#
# fp2 = open("hh.txt", "a")
# fp2.write("aadsafsaufoidsu")

# fp = open("hh.txt","r")
# print(fp.read())
# # fp.write("aa")

# fo = open("test.txt", "w")
# print("文件名为: ", fo.name)
# str = "菜鸟教程"
# fo.write(str)
#
# # 关闭文件
# fo.close()

# fp = open("hh.txt","w")
# fp.write("abcd")
# fp.close()

# with open("example.txt","r") as fp:
#     fp.read(5)

# fp = open("example.txt",encoding="utf-8")
# print(fp.read())
# fp.close()

#  准备一个example.txt文件， 里面随便写入些内容(如一首歌词)
#  读前5行。
#  读全文
#  读取最后12字节 => str格式
#  读取最后4个汉字 => str格式

with open("example.txt","r",encoding="utf-8") as fp:
    # 读前5行
    for i in range(5):
        print(fp.readline())
    # 读全文
    print("**********")
    fp.seek(0)
    print(fp.read())
    print("**********")

    # fp.readline()
# 读取最后12字节 => str格式

fp1 = open("example.txt","rb")
fp1.seek(-12,2)
print(fp1.read().decode("utf-8"))
fp1.close()
print("*************")
# 读取最后4个汉字 => str格式
fp2 = open("example.txt","rb")
fp2.seek(-16,2)
print(fp2.read().decode("utf-8"))
fp2.close()

print("*************")
# 读取一个文件,显示除了以井号(#)开头的行以外的所有行。
with open("example.txt","r",encoding="utf-8") as fp3:
    for i in fp3:
        if i[0] == "#":
            pass
        else:
            print(i)



